package com.example.onlineeducation.ui.auth

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.view.View
import android.view.inputmethod.EditorInfo
import androidx.activity.ComponentActivity
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import com.example.onlineeducation.BuildConfig
import com.example.onlineeducation.R
import com.example.onlineeducation.base.BaseActivity
import com.example.onlineeducation.data.model.CertificateListDataModel
import com.example.onlineeducation.data.model.RegisterSearchGroupListDataModel
import com.example.onlineeducation.data.model.RegisterSearchProjectListDataModel
import com.example.onlineeducation.databinding.*
import com.example.onlineeducation.utils.YcLoopCountDown
import com.example.onlineeducation.viewmodel.LoginVM
import com.example.onlineeducation.viewmodel.RegisterVM
import com.example.onlineeducation.widget.YcAlbumOrCameraPopupWindow
import com.gyf.immersionbar.BarHide
import com.gyf.immersionbar.ImmersionBar
import com.yc.jetpacklib.extension.showToast
import com.yc.jetpacklib.extension.ycInitLinearLayoutManage
import com.yc.jetpacklib.extension.ycIsNotEmpty
import com.yc.jetpacklib.extension.ycLoadImageNet
import com.yc.jetpacklib.manager.YcActivityManager
import com.yc.jetpacklib.net.doSuccess
import com.yc.jetpacklib.recycleView.YcPagingDataAdapter
import com.yc.jetpacklib.refresh.YcRefreshBaseUtil
import com.yc.jetpacklib.refresh.YcRefreshSpecialPagingUtil
import com.yc.jetpacklib.utils.YcRegularUtil
import com.yc.jetpacklib.utils.YcRegularUtil.ycRegular

import java.util.*

/**
 *  注册项目列表
 */
class RegisterSearchGroupActivity :
    BaseActivity<RegisterProjectListAcitvityBinding>(RegisterProjectListAcitvityBinding::inflate) {

    companion object {
        private const val KEY_PRO_ID = "key_pro_id"

        const val KEY_ID = "key_id"
        const val KEY_NAME = "key_name"
        fun newInstance(activity: Context,proId: String, launcher: ActivityResultLauncher<Intent>) {
            val intent = Intent(activity, RegisterSearchGroupActivity::class.java).apply{
                putExtra(KEY_PRO_ID, proId)
            }
            launcher.launch(intent)
        }
        @JvmStatic
        inline fun getLauncher(activity: ComponentActivity, crossinline block: (id: String, name: String) -> Unit): ActivityResultLauncher<Intent> {
            return activity.registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
                if (it.resultCode == RESULT_OK && it.data != null) {
                    val id = it.data?.getStringExtra(KEY_ID)!!
                    val name = it.data?.getStringExtra(KEY_NAME)!!
                    block.invoke(id,name)
                }
            }
        }
    }

    private val mAdapter: YcPagingDataAdapter<RegisterSearchGroupListDataModel, RegisterSearchItemBinding> by YcPagingDataAdapter.ycLazyInitApply(
        RegisterSearchItemBinding::inflate, RegisterSearchGroupListDataModel.diffCallback
    ) {
        mOnUpdate = { item ->
            tvProName.text = item.groupName
        }
        mItemClick = {
            val intent = Intent()
            intent.putExtra(KEY_ID, it.groupId)
            intent.putExtra(KEY_NAME, it.groupName)
            setResult(RESULT_OK, intent)
            finish()
        }
    }

    private val mViewModel: RegisterVM by ycViewModels()
    private lateinit var mRefreshUtil: YcRefreshBaseUtil<RegisterSearchGroupListDataModel>

    override fun RegisterProjectListAcitvityBinding.initView() {
        actionbarLayout.setTitle("班组搜索")
        etSearch.hint = "搜索班组"
        etSearch.setOnEditorActionListener { _, keyCode, _ ->
            when (keyCode) {
                EditorInfo.IME_ACTION_SEARCH -> mRefreshUtil.startRefresh(true)
            }
            false
        }
        tvCancel.setOnClickListener {
            finish()
        }
        rvProList.apply {
            adapter = mAdapter
            ycInitLinearLayoutManage()
        }
        mRefreshUtil =
            YcRefreshSpecialPagingUtil<RegisterSearchGroupListDataModel>(this@RegisterSearchGroupActivity)
                .build(mAdapter, srlList, rvProList, flRefresh) {
                    mRefreshCall = {
                        getData()
                    }
                }
        mRefreshUtil.startRefresh()
        mViewModel.mGroupList.observe {
            mRefreshUtil.acSetPagingData(it)
        }
    }

    private fun RegisterProjectListAcitvityBinding.getData() {
        mViewModel.getGroupList(etSearch.text.toString(),intent.getStringExtra(KEY_PRO_ID)!!)
    }


}